//
//  BDHomeViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/11.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDHomeViewController.h"
#import "BDHomeHeaderView.h"
#import "BDHomeCell.h"
#import "BDAllBusinessViewController.h"
#import "BDMerchantManageViewController.h"
#import "BDAddBusinessInfoViewController.h"
#import "UIViewController+BDAddBusinessHelper.h"

#import "AppDelegate.h"

static NSString *const cellReuseIdentifier = @"cellReuseIdentifier";

static float kHeaderHeight = 207.0;
static float kBottomTabBarHegith = 49.0;


@interface BDHomeViewController ()<BDHomeHeaderViewDelegate>

@property (nonatomic, copy) NSArray *dataList;

@end

@implementation BDHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataList = @[@{@"image":@"loan",@"title":@"所有商户"},
                      @{@"image":@"Credit",@"title":@"运营数据"},
                      @{@"image":@"bd_logout",@"title":@"退出"}]; //展示信息数据源

    [self setupHeaderView]; //头部视图
    [self setupCollectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    self.navigationController.navigationBar.shadowImage = nil;
}

#pragma mark- super methods -
- (UIEdgeInsets)hsy_collectionViewEdgeInsets{
    return UIEdgeInsetsMake(kHeaderHeight, 0, kBottomTabBarHegith, 0);
}

- (BOOL)hsy_allowPullUp{
    return NO;
}

- (BOOL)hsy_allowPullDown{
    return NO;
}

#pragma mark - init subviews -
//更新头部视图
- (void)setupHeaderView{
    BDHomeHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"BDHomeHeaderView" owner:nil options:nil] firstObject];
    headerView.delegate = self;
    
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.height.mas_equalTo(kHeaderHeight);
    }];
}

//定义collecitonView的布局
- (void)setupCollectionView{
    UICollectionViewFlowLayout *flowLayout = (id)self.collectionView.collectionViewLayout;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake((ScreenWidth()-2)/3,(ScreenWidth()-2)/3);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"BDHomeCell" bundle:nil] forCellWithReuseIdentifier:cellReuseIdentifier];
    
    self.collectionView.backgroundColor = [UIColor hsyDefaultBackgroudColor];
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource & UICollectionViewDelegateFlowLayout -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.dataList[indexPath.item];
    
    BDHomeCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:cellReuseIdentifier forIndexPath:indexPath];
    
    item.imageView.image = [UIImage imageNamed:dict[@"image"]];
    item.titleLabel.text = dict[@"title"];
    
    return item;
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {//所有商户
        [self allBusiness];
    }
    
    if (indexPath.row == 1) { //运营数据
        [self outcomes];
    }
    
    if (indexPath.row == 2) { //退出登录
        [self logout];
    }
}


//所有商户
- (void)allBusiness{
    BDAllBusinessViewController *vc = [[BDAllBusinessViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];
}

//运营数据
- (void)outcomes{
    
}

//退出登录
- (void)logout{
    [BDUserAccount removeSaveSelf];
    [[AppDelegate delfaultDelegate] goToBDLoginViewController];
}

//添加报单
- (void)handleAddReport{
    BDAddBusinessInfoViewController *vc = [[BDAddBusinessInfoViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];

}

//报单中的商户
- (void)handleShowBussinessInReport{
    BDMerchantManageViewController *vc = [[BDMerchantManageViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
